package com.sdruixinggroup.mondayb2b.ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.google.gson.reflect.TypeToken;
import com.sdruixinggroup.mondayb2b.API.API;
import com.sdruixinggroup.mondayb2b.R;
import com.sdruixinggroup.mondayb2b.adapter.QuickAdapter;
import com.sdruixinggroup.mondayb2b.interfaces.ObjectCallBack;
import com.sdruixinggroup.mondayb2b.models.NearbyShop;
import com.sdruixinggroup.mondayb2b.utils.UserInfoUtil;
import com.zhy.http.okhttp.OkHttpUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import me.leefeng.lfrecyclerview.LFRecyclerView;
import okhttp3.Call;

/**
 * Created by tangjie on 2017/11/11.
 */

public class MoreShopsActivity extends BaseActivity {
    @BindView(R.id.ib_back)
    ImageView ibBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.ib_setting)
    ImageView ibSetting;
    @BindView(R.id.recyclerView)
    LFRecyclerView recyclerView;
    @BindView(R.id.loading)
    ProgressBar loading;
    private MyAdapter myAdapter;
    private List<NearbyShop.MerchantsBean.DataBean> dataBeans;
    private int  page = 1;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.common_fragment_layout);
        ButterKnife.bind(this);


        tvTitle.setText("附近门店");

        ibSetting.setImageResource(R.drawable.home_search);
        ibSetting.setVisibility(View.VISIBLE);
        ibSetting.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String parms = "shopsearch?" + "&area_id=" + UserInfoUtil.AREA_CODE + "&latitude=" + UserInfoUtil.mLatitude
                        + "&longitude=" +UserInfoUtil.mLongitude;
                toWebView(parms);
            }
        });
        myAdapter = new MyAdapter(MoreShopsActivity.this);
        dataBeans = new ArrayList<>();
        myAdapter.setData(dataBeans);
        recyclerView.setLoadMore(true);
        recyclerView.setRefresh(true);
        recyclerView.setAdapter(myAdapter);
        myAdapter.setOnItemClickListener(new QuickAdapter.OnItemClickListener<NearbyShop.MerchantsBean.DataBean>(){
            @Override
            public void OnItemClick(NearbyShop.MerchantsBean.DataBean bean, int postion) {
                String params = "shopinfo?merchant_id="+bean.getMerchant_id()+"&area_id="+ UserInfoUtil.AREA_CODE +"&latitude="+bean.getLatitude()
                        +"&longitude="+bean.getLongitude();
                toWebView(params);
            }
        });
        recyclerView.setLFRecyclerViewListener(new LFRecyclerView.LFRecyclerViewListener(){
            @Override
            public void onRefresh() {
                queryData(true);
            }

            @Override
            public void onLoadMore() {
                queryData(false);
            }
        });
        queryData(true);
    }

    @OnClick(R.id.ib_back)
    public void onViewClicked() {
        finish();
    }

    private void queryData(final  boolean isReflush){

        if(isReflush){
            page=1;
        }
        loading.setVisibility(View.VISIBLE);
        String url = API.MERCHANTS_NEARBY +"page="+page+"&page_size=10"+ "&area_id="+ UserInfoUtil.AREA_CODE +"&latitude="+UserInfoUtil.mLatitude
                +"&longitude="+UserInfoUtil.mLongitude+"&token="+UserInfoUtil.getInfoToken(getApplication());
        HashMap<String,String> headers = new HashMap<>();
        headers.put("Accept","*/*");

        TypeToken<NearbyShop> typeToken =new TypeToken<NearbyShop>(){};
        OkHttpUtils.get().url(url).headers(headers).build().execute(new ObjectCallBack<NearbyShop>(typeToken) {
            @Override
            public void onError(Call call, Exception e, int id) {

                if(null != loading){
                    loading.setVisibility(View.GONE);
                }
                if(null != recyclerView){
                    recyclerView.stopRefresh(false);
                    recyclerView.stopLoadMore();
                }
            }

            @Override
            public void onResponse(NearbyShop response, int id) {
                if(isReflush){
                    dataBeans.clear();
                }
                if(null != response){
                    if(response.getErr_code() == 0 ){
                        List<NearbyShop.MerchantsBean.DataBean> merchantsBeans= response.getMerchants().getData();
                        if(null != merchantsBeans && !merchantsBeans.isEmpty()){
                            dataBeans.addAll(merchantsBeans);
                            ++page;
                        }
                    }else {
                        showToast(response.getErr_msg());
                    }
                }
                myAdapter.reflashData(dataBeans);
                if (null != recyclerView) {
                    recyclerView.stopRefresh(true);
                    recyclerView.stopLoadMore();
                }

                if (null != loading) {
                    loading.setVisibility(View.GONE);
                }
            }
        });
    }

    private void toWebView(String param) {
        Intent intent = new Intent(getBaseContext(), WebActivity.class);
        intent.putExtra(WebActivity.INTENT_EXTRA_BEGIN_URL, param);
        startActivity(intent);
    }
}
